package com.jp.knowledge.view.maintab;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.jp.knowledge.R;

/**
 * Created by 528787067@qq.com on 2017/7/20.
 */

public class MainTabView extends LinearLayout implements View.OnClickListener {

	private Paint paint;
	private TabSelectedListener tabSelectedListener;

	public MainTabView(Context context) {
		super(context);
		init(context);
	}

	public MainTabView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MainTabView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init(context);
	}

	private void init(Context context){
		setTag(-1);
		setWillNotDraw(false);
		setOrientation(HORIZONTAL);
		setGravity(Gravity.CENTER);
		setPadding(getPaddingLeft(),
				getPaddingTop() + (int)(context.getResources().getDimension(R.dimen.line_heigth)+0.5),
				getPaddingRight(),
				getPaddingBottom()
		);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(context.getResources().getColor(R.color.line_gray));
		paint.setStrokeWidth(context.getResources().getDimension(R.dimen.line_heigth));
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawLine(0, 0, getWidth(), 0, paint);
	}

	public boolean addTab(@NonNull MainTabItemView tabItemView){
		return addTab(tabItemView, getTabCount());
	}

	public boolean addTab(@NonNull MainTabItemView tabItemView, int position){
		if(position < 0 || position > getTabCount())
			return false;
		tabItemView.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f));
		tabItemView.setOnClickListener(this);
		if(getChildCount() == 0)
			tabItemView.selectState();
		else
			tabItemView.unselectState();
		addView(tabItemView, position);
		if(getTabCount() == 1)
			setSelectIndex(0);
		return true;
	}

	public boolean removeTab(int position){
		if(position < 0 || position >= getChildCount())
			return false;
		removeViewAt(position);
		if(getChildCount() == 0)
			setTag(-1);
		else if(position == (int)getTag())
			onClick(getChildAt(0));
		return true;
	}

	public int removeTab(MainTabItemView tabItemView){
		int position = indexOfChild(tabItemView);
		if(position >= 0)
			removeTab(position);
		return position;
	}

	public void removeAllTab(){
		removeAllViews();
		setTag(-1);
	}

	public int getTabCount(){
		return getChildCount();
	}

	public MainTabItemView getTab(int position){
		try {
			return (MainTabItemView) getChildAt(position);
		} catch (Exception e){
			return null;
		}
	}

	public int indexOfTab(MainTabItemView tabItemView){
		return indexOfChild(tabItemView);
	}

	public int getSelectIndex(){
		return (int) getTag();
	}

	public void setSelectIndex(int index){
		if(index < 0 || index >= getTabCount())
			return;
		onClick(getTab(index));
	}

	@Override
	public void onClick(View v) {
		int unselectPosition = (int) getTag();
		int selectPosition = indexOfChild(v);
		if(selectPosition == unselectPosition){
			if(tabSelectedListener != null)
				tabSelectedListener.onTabReselect(selectPosition);
		} else {
			setTag(selectPosition);
			MainTabItemView unselectTab = getTab(unselectPosition);
			MainTabItemView selectTab = getTab(selectPosition);
			if(unselectTab != null)
				unselectTab.unselectState();
			if(selectTab != null)
				selectTab.selectState();
			if(tabSelectedListener != null)
				tabSelectedListener.onTabSelect(unselectPosition, selectPosition);
		}
	}

	public void setTabSelectedListener(TabSelectedListener tabSelectedListener){
		this.tabSelectedListener = tabSelectedListener;
	}

	public interface TabSelectedListener{
		void onTabSelect(int oldPosition, int newPosition);
		void onTabReselect(int position);
	}

	abstract public static class MainTabItemView extends FrameLayout{

		public MainTabItemView(Context context) {
			super(context);
			init();
		}

		public MainTabItemView(Context context, AttributeSet attrs) {
			super(context, attrs);
			init();
		}

		public MainTabItemView(Context context, AttributeSet attrs, int defStyleAttr) {
			super(context, attrs, defStyleAttr);
			init();
		}

		protected void init(){
			addView(setTabView());
		}

		public <T extends View> T getView(int layoutResID){
			return (T) findViewById(layoutResID);
		}

		abstract public View setTabView();
		abstract public void selectState();
		abstract public void unselectState();
	}
}
